package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.dto.ServiceInfoDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.InsertServiceInfoForm;
import com.woniu108.product.form.UpdateServiceInfoForm;
import com.woniu108.product.param.*;
import com.woniu108.product.service.ServiceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务信息表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/serviceInfo")
@Api(tags = "服务信息接口")
public class ServiceInfoController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ServiceInfoService serviceInfoService;

    /**
     * 查所有的服务信息，或单个种类服务信息
     *
     * @param sTid 服务种类编号
     * @return 店家服务信息集合
     */
    @GetMapping("/findAllServiceInfo.c")
    @ApiOperation("查所有服务信息，或单个种类服务信息，或门店编号")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "sTid", name = "服务种类编号"),
            @ApiImplicitParam(value = "storeId", name = "门店编号")
    }
    )
    public Result<List<ServiceInfoDto>> findAllServiceInfo(Integer sTid, Integer storeId) {
        List<ServiceInfoDto> list = serviceInfoService.findAllServiceInfo(sTid,storeId);
        return Result.newListInstance(ServiceInfoDto.class).setData(list).setMsg("查所有服务信息成功");
    }

    /**
     * 通过服务id查服务信息
     *
     * @param serviceId 服务id
     * @return 服务信息
     */
    @GetMapping("/findServiceInfoById.c")
    @ApiOperation("通过服务id查服务信息")
    @ApiImplicitParam(value = "serviceId", name = "服务信息编号")
    public Result<ServiceInfoDto> findServiceInfoById(Integer serviceId) {
        ServiceInfoDto dto = serviceInfoService.findServiceInfoById(serviceId);
        return Result.newInstance(ServiceInfoDto.class).setData(dto).setMsg("通过服务id查服务信息成功");
    }

    /**
     * 查店家自己的服务信息
     *
     * @return 店家服务信息集合
     */
    @GetMapping("/findMyServiceInfo.st")
    @ApiOperation("查店家自己的服务信息")
    public Result<List<ServiceInfoDto>> findMyServiceInfo(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        List<ServiceInfoDto> list = serviceInfoService.findMyServiceInfo(storeAdminId);
        return Result.newListInstance(ServiceInfoDto.class).setData(list).setMsg("查店家自己的服务信息成功");
    }

    /**
     * 新增服务信息
     *
     * @param form 服务名称,服务种类编号,服务描述,基础总服务费
     * @return boolean值
     */
    @PostMapping("/insertServiceInfo.st")
    @ApiOperation("新增服务信息")
    public Result<Boolean> insertServiceInfo(@RequestBody InsertServiceInfoForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        InsertServiceInfoParam param = BeanUtil.toBean(form, InsertServiceInfoParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean bool = serviceInfoService.insertServiceInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增服务信息成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增服务信息失败").setData(false).setCode(500);
    }

    /**
     * 修改服务信息
     *
     * @param form 服务id,新服务名称,新服务描述,新基础总服务费
     * @return boolean值
     */
    @PostMapping("/updateServiceInfo.st")
    @ApiOperation("修改服务信息")
    public Result<Boolean> updateServiceInfo(@RequestBody UpdateServiceInfoForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        UpdateServiceInfoParam param = BeanUtil.toBean(form, UpdateServiceInfoParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean bool = serviceInfoService.updateServiceInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改服务信息成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改服务信息失败").setData(false).setCode(500);
    }

    /**
     * 禁用服务信息
     *
     * @param serviceId 服务id
     * @return boolean值
     */
    @GetMapping("/banServiceInfo.st")
    @ApiOperation("禁用服务信息")
    @ApiImplicitParam(value = "serviceId", name = "服务信息id")
    public Result<Boolean> banServiceInfo(Integer serviceId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        BanServiceInfoParam param = new BanServiceInfoParam(serviceId, storeAdminId);
        Boolean bool = serviceInfoService.banServiceInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用服务信息成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用服务信息失败").setData(false).setCode(500);
    }

    /**
     * 解锁服务信息
     *
     * @param serviceId 服务id
     * @return boolean值
     */
    @GetMapping("/unlockServiceInfo.st")
    @ApiOperation("解锁服务信息")
    @ApiImplicitParam(value = "serviceId", name = "服务信息id")
    public Result<Boolean> unlockServiceInfo(Integer serviceId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        UnlockServiceInfoParam param = new UnlockServiceInfoParam(serviceId, storeAdminId);
        Boolean bool = serviceInfoService.unlockServiceInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁服务信息成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁服务信息失败").setData(false).setCode(500);
    }

    /**
     * 删除服务详情
     *
     * @param serviceId 服务id
     * @return boolean值
     */
    @GetMapping("/deleteServiceInfo.st")
    @ApiOperation("删除服务详情")
    @ApiImplicitParam(value = "serviceId", name = "服务信息id")
    public Result<Boolean> deleteServiceInfo(Integer serviceId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        DeleteServiceInfoParam param = new DeleteServiceInfoParam(serviceId, storeAdminId);
        Boolean bool = serviceInfoService.deleteServiceInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除服务详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除服务详情失败").setData(false).setCode(500);
    }

    /**
     * 通过流程ID查服务详情是否存在
     *
     * @param streamId 流程ID
     * @return boolean值
     */
    @GetMapping("/findServiceInfoByStreamId.c")
    @ApiOperation("通过流程ID查服务详情是否存在")
    @ApiImplicitParam(value = "streamId", name = "流程ID")
    public Result<Boolean> findServiceInfoByStreamId(Integer streamId) {
        Boolean bool = serviceInfoService.findServiceInfoByStreamId(streamId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("查服务详情是否存在成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("查服务详情是否存在失败").setData(false).setCode(500);

    }
}

